﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DanhMucSanPhamProvider
/// CuongBH
/// 07/12/2011
/// </summary>
/// 
namespace EHTWebsite.Code
{
    public class DanhMucSanPhamProvider
    {
        public static Int32 IUDanhMucSanPham(DanhMucSanPham theDanhMucSanPham, bool IsAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IUDanhMucSanPham";
            command.CommandType = CommandType.StoredProcedure;
            SqlParameter theDanhMucIDParameter = new SqlParameter();

            theDanhMucIDParameter.ParameterName = "@DanhMucIDOut";
            theDanhMucIDParameter.Direction = ParameterDirection.Output;
            theDanhMucIDParameter.DbType = DbType.Int32;

            command.Parameters.Add(theDanhMucIDParameter);
            command.Parameters.Add(new SqlParameter("@DanhMucID", theDanhMucSanPham.DanhMucID));
            command.Parameters.Add(new SqlParameter("@TenDanhMucVN", theDanhMucSanPham.TenDanhMucVN));
            command.Parameters.Add(new SqlParameter("@TenDanhMucOther", theDanhMucSanPham.TenDanhMucOther));
            command.Parameters.Add(new SqlParameter("@SapXep", theDanhMucSanPham.SapXep));
            command.Parameters.Add(new SqlParameter("@LuongTruyCap", theDanhMucSanPham.LuongTruyCap));
            command.Parameters.Add(new SqlParameter("@ChuyenMucChaID",theDanhMucSanPham.ChuyenMucChaID));
            command.Parameters.Add(new SqlParameter("@Publish",theDanhMucSanPham.Publish));
            command.Parameters.Add(new SqlParameter("@DeletedFlag",theDanhMucSanPham.DeletedFlag));
            command.Parameters.Add(new SqlParameter("@IsAdd", IsAdd));
            DataProvider.ExecuteNoneQuery(command);

            return Int32.Parse(command.Parameters["@DanhMucIDOut"].Value.ToString());
        }

        /// <summary>
        /// Author : toanvt
        /// CreatedDate : 16/02/2012
        /// Description : Xóa Danh mục sản phẩm
        /// </summary>
        /// <param name="danhMucID"></param>
        /// <returns></returns>
        public static void DeleteDanhMucSanPhamByDanhMucID(Int32 DanhMucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteDanhMucSanPhamByDanhMucID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucID", DanhMucID));

            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// Author : toanvt
        /// CreatedDate : 16/02/2012
        /// Description : lấy danh sách Danh Mục Sản Phẩm cho phần FontEnd
        /// </summary>
        /// <param name="danhMucID"></param>
        /// <returns></returns>
        public static DataSet GetDanhMucSanPhamsForFrontEnd(Int32 danhMucID, Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetDanhMucSanPhamsForFrontEnd";
            command.Parameters.Add(new SqlParameter("@DanhMucID", danhMucID));
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command, out totalRecord);
        }

        /// <summary>
        /// Author : toanvt
        /// CreatedDate : 16/02/2012
        /// Description : lấy danh sách Danh Mục Sản Phẩm cho phần quản trị
        /// </summary>
        /// <param name="danhMucID"></param>
        /// <returns></returns>
        public static DataSet GetDanhMucSanPhamsForBackEnd(Int32 danhMucID, Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetDanhMucSanPhamsForBackEnd";
            command.Parameters.Add(new SqlParameter("@DanhMucID", danhMucID));
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command, out totalRecord);
        }

        public static void UpdateLuotTruyCap(Int32 DanhMucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_UDanhMucSanPham_SoLuongTruyCap";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucID", DanhMucID));

            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// Author : toanvt
        /// CreateDate : 16/02/2012
        /// Descrition : 
        /// </summary>
        /// <param name="DanhMucID"></param>
        /// <returns></returns>
        public static DanhMucSanPham GetDanhMucSanPhamByDanhMucID(Int32 DanhMucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetDanhMucSanPhamByDanhMucID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucID", DanhMucID));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            DanhMucSanPham theDanhMucSanPham = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theDanhMucSanPham = new DanhMucSanPham();
                theDanhMucSanPham.DeletedFlag = Boolean.Parse(ds.Tables[0].Rows[0]["DeletedFlag"].ToString());
                theDanhMucSanPham.TenDanhMucVN = ds.Tables[0].Rows[0]["TenDanhMucVN"].ToString();
                theDanhMucSanPham.TenDanhMucOther = ds.Tables[0].Rows[0]["TenDanhMucOther"].ToString();
                theDanhMucSanPham.SanPhamHotID = Int32.Parse(ds.Tables[0].Rows[0]["SanPhamHotID"].ToString());
                theDanhMucSanPham.LuongTruyCap = Int32.Parse(ds.Tables[0].Rows[0]["LuongTruyCap"].ToString());
                theDanhMucSanPham.SapXep = Int32.Parse(ds.Tables[0].Rows[0]["SapXep"].ToString());
                theDanhMucSanPham.DanhMucID = Int32.Parse(ds.Tables[0].Rows[0]["DanhMucID"].ToString());
                theDanhMucSanPham.ChuyenMucChaID = Int32.Parse(ds.Tables[0].Rows[0]["ChuyenMucChaID"].ToString());
                theDanhMucSanPham.Publish = Boolean.Parse(ds.Tables[0].Rows[0]["Publish"].ToString());
            }

            return theDanhMucSanPham;
        }


        /// <summary>
        /// Author:tungvu90
        /// Create Date:07/02/2012
        /// Descripttion:Update danh mục sản phẩm theo numbetoforder
        /// </summary>
        /// <param name="DanhMucKhachHangID"></param>
        /// <param name="SapXep"></param>
        /// <returns></returns>
        public static DataSet UpdateNumberOfOrderByDanhMucSanPhamId(Int32 DanhMucID, Int32 SapXep)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_UpdateNumberOfOrderbyDanhMucSanPhamId";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucID", DanhMucID));
            command.Parameters.Add(new SqlParameter("@SapXep", SapXep));
            return DataProvider.ExecuteDataSet(command);
        }

        /// <summary>
        /// Author:tungvu90
        /// Create date:07/02/2012
        /// Description:Update danh mục sản phẩm theo Publish
        /// </summary>
        /// <param name="DanhMucID"></param>
        public static void ChangePublishByDanhMucSanPhamID(Int32 DanhMucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_ChangePublishByDanhMucSanPhamID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucID", DanhMucID));
            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// Author:tungvu90
        /// Create Date:07/02/2012
        /// Description:Lấy danh sách chuyên mục sản phẩm
        /// </summary>
        /// <param name="DanhMucID"></param>
        /// <returns></returns>
        public static DataSet GetDanhMucDanhMucSanPhamsByNhomDanhMucID(Int32 DanhMucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetDanhMucSanPhamsByNhomDanhMucID";
            command.Parameters.Add(new SqlParameter("@DanhMucID", DanhMucID));
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="DanhMucID"></param>
        /// <returns></returns>
        public static DataSet GetSanPhambyDanhMucSanphamID(Int32 DanhMucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetTinTucByDanhMucSanPhamID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucID", DanhMucID));
            DataSet ds = DataProvider.ExecuteDataSet(command);
            return ds;
        }
    }
}